package com.wwy.hkapi.HKApi;


import com.wwy.hkapi.HKApi.exception.DvrChannelErrorException;
import com.wwy.hkapi.HKApi.exception.DvrNosupportException;
import com.wwy.hkapi.HKApi.init.HkInit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * @Author: wwy
 * @Date: 2020/8/6 14:09
 */
public class HKImageCapture {
    private static final Logger LOGGER = LoggerFactory.getLogger(HKImageCapture.class);
    /**
     * 截图
     * @param userId
     * @param channel
     * @param filePath 图片路径加名称
     */
    public boolean imageCapture(int userId,int channel,String filePath){
        //获取设备通道开始数

        if(filePath == null){
            return false;
        }
        filePath = filePath.replace("/","\\");
        String filePathCatalogue = filePath.substring(0,filePath.lastIndexOf("\\"))+"\\";
        String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
        File file = new File(filePathCatalogue);
        if(!file.exists()){
            file.mkdirs();
        }
        /*//获取能力级
        String strPathCrypto = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n" +
                "<!--req, 获取JPEG抓图能力时pInBuf参数描述--> \n" +
                "<JpegCaptureAbility version=\"2.0\"> \n" +
                "        <channelNO>"+channel+"</channelNO><!--req,通道号--> \n" +
                "</JpegCaptureAbility> \n";
        HCNetSDK.BYTE_ARRAY ptrByteArrayCrypto = new HCNetSDK.BYTE_ARRAY(strPathCrypto.length());
        ptrByteArrayCrypto.byValue= strPathCrypto.getBytes();
        Pointer pInBuf = ptrByteArrayCrypto.getPointer();
        //结构体内存转移到Ponter
        ptrByteArrayCrypto.write();
        //出参的指针
        HCNetSDK.BYTE_ARRAY ptrByteArrayCryptoUt = new HCNetSDK.BYTE_ARRAY(1024);
        Pointer pOutBuf = ptrByteArrayCryptoUt.getPointer();
        HkInit.hCNetSDK.NET_DVR_GetDeviceAbility(userId,0x00f,pInBuf,1024,pOutBuf,10240);
        ptrByteArrayCryptoUt.read();////Pointer内存转移到结构体
        String outXml = new String(ptrByteArrayCryptoUt.byValue);
        LOGGER.info("通道号：{},设备能力级返回信息",channel,outXml);*/
        HCNetSDK.NET_DVR_JPEGPARA netDvrJpegpara = new HCNetSDK.NET_DVR_JPEGPARA();
        netDvrJpegpara.wPicSize = 0xff;
        netDvrJpegpara.wPicQuality = 2;
        boolean b = HkInit.hCNetSDK.NET_DVR_CaptureJPEGPicture(userId, channel, netDvrJpegpara, filePathCatalogue+fileName);
        int i = HkInit.hCNetSDK.NET_DVR_GetLastError();
        LOGGER.info("截屏错误码：{},备注0没有错误",i);
        if(i == 23 || i==29){
            //设备不支持
            throw new DvrNosupportException("Equipment not supported");
        }else if(i == 4){
            //通道号错误
            throw new DvrChannelErrorException("channel error");
        }

        if(b){
            LOGGER.info("截屏成功，路径:{}",filePath);
        }else{
            LOGGER.info("截屏失败");
        }
        return b;
    }
}
